import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_stort/config/routes.dart';
import 'package:flutter_stort/utils/cache_utils.dart';
import 'package:flutter_stort/utils/dialog_utils.dart';
import 'package:flutter_stort/utils/jump_util.dart';
import 'package:flutter_stort/utils/toast_utils.dart';

class SettingPage extends StatefulWidget {
  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "设置",
          style: TextStyle(
            fontSize: 20,
          ),
        ),
      ),
      body: Container(
        color: Colors.grey.withOpacity(0.2),
        child: ListView(
          children: [
            feedbackView(),
            softwareEvaluationView(),
            clearCacheView(),
            currentVersionView(),
            aboutView(),
            logoutView(),
          ],
        ),
      ),
    );
  }
}

class aboutView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
      height: 55,
      child: InkWell(
        onTap: () => {JumpPageUtils.startNext(context, RoutesUtils.ABOUT_ROOT)},
        child: Row(
          children: [
            Expanded(
              child: Text(
                "关于我们",
                style: TextStyle(color: Colors.black, fontSize: 16),
              ),
            ),
            Icon(
              Icons.navigate_next,
              color: Colors.grey,
            ),
          ],
        ),
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 0.5,
            style: BorderStyle.solid,
          ),
        ),
      ),
    );
  }
}

class feedbackView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
      height: 55,
      child: InkWell(
        onTap: () {
          JumpPageUtils.startNext(context, RoutesUtils.FEEDBACK_ROOT);
        },
        child: Row(
          children: [
            Expanded(
              child: Text(
                "意见反馈",
                style: TextStyle(color: Colors.black, fontSize: 16),
              ),
            ),
            Icon(
              Icons.navigate_next,
              color: Colors.grey,
            ),
          ],
        ),
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 0.5,
            style: BorderStyle.solid,
          ),
        ),
      ),
    );
  }
}

class clearCacheView extends StatefulWidget {
  @override
  _clearCacheViewState createState() => _clearCacheViewState();
}

class _clearCacheViewState extends State<clearCacheView> {
  CacheUtils cacheUtils = CacheUtils();
  String cacheData = "";
  bool isVisible = true;

  @override
  void initState() {
    if (Platform.isIOS) {
      isVisible = false;
    } else if (Platform.isAndroid) {
      isVisible = true;
    }
    cacheUtils.loadCache().then((value) {
      setState(() {
        cacheData = value;
        print(cacheData);
      });
    });
    super.initState();
  }

  ///清除缓存
  void _clearCache() {
    cacheUtils.clearCache();
    ToastUtil.showToast("缓存清除成功！");
    setState(() {
      cacheData = "";
    });
  }

  @override
  Widget build(BuildContext context) {
    return Visibility(
      visible: isVisible,
      child: Container(
        height: 55,
        padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
        child: InkWell(
          onTap: () {
            DialogUtils.showCommonDialog(context, "提示", "您确认要清除该缓存吗？", () {
             JumpPageUtils.closeDialog(context);
              _clearCache();
            });
          },
          child: Row(
            children: [
              Expanded(
                child: Text(
                  "清除缓存",
                  style: TextStyle(color: Colors.black, fontSize: 16),
                ),
              ),
              Text(
                cacheData,
                style: TextStyle(color: Colors.grey, fontSize: 14),
              ),
              Icon(Icons.navigate_next, color: Colors.grey),
            ],
          ),
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(
              color: Colors.grey,
              width: 0.5,
              style: BorderStyle.solid,
            ),
          ),
        ),
      ),
    );
  }
}

class currentVersionView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 55,
      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      child: InkWell(
        onTap: () {
          DialogUtils.showCommonDialog(context, "软件更新", "1.修改了BUG \n 2.优化程序", () {
            JumpPageUtils.closeDialog(context);
          });
        },
        child: Row(
          children: [
            Expanded(
              child: Text(
                "当前版本号",
                style: TextStyle(color: Colors.black, fontSize: 16),
              ),
            ),
            Text(
              "v1.0.0",
              style: TextStyle(color: Colors.grey, fontSize: 14),
            ),
          ],
        ),
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 0.5,
            style: BorderStyle.solid,
          ),
        ),
      ),
    );
  }
}

class softwareEvaluationView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
      height: 55,
      child: InkWell(
        onTap: () {
          DialogUtils.showSoftwareScore(context);
        },
        child: Row(
          children: [
            Expanded(
              child: Text(
                "软件评分",
                style: TextStyle(color: Colors.black, fontSize: 16),
              ),
            ),
            Icon(
              Icons.navigate_next,
              color: Colors.grey,
            ),
          ],
        ),
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 0.5,
            style: BorderStyle.solid,
          ),
        ),
      ),
    );
  }
}

///退出登录
class logoutView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        DialogUtils.showCommonDialog(context, "提示", "您确认要退出程序吗?", () {
          Navigator.of(context).pushNamedAndRemoveUntil(
            RoutesUtils.LOGIN_ROOT,
            ModalRoute.withName(RoutesUtils.LOGIN_ROOT),
          );
        });
      },
      child: Container(
        color: Colors.white,
        alignment: Alignment.center,
        height: 50,
        margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
        padding: EdgeInsets.all(10),
        child: Text(
          "退出登录",
          style: TextStyle(color: Colors.black, fontSize: 16),
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}
